﻿#region USING STATEMENTS
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Fusion.Content;
#endregion

#region CODE BODY
namespace Fusion.Management
{
    public static class FTextures
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private static List<GameTexture> Textures = new List<GameTexture>();

        //////////////////////////////////////////////////
        // Public Static Methods
        //////////////////////////////////////////////////
        public static GameTexture AddTexture(String TextureFile)
        {
            string NameOfFile = Path.GetFileName(TextureFile);

            foreach (GameTexture Texture in Textures)
            {
                if (Texture != null)
                {
                    if (Texture.Name.Equals(NameOfFile))
                    {
                        Texture.AddRef();
                        return Texture;
                    }
                }
            }

            int Index = -1;

            if (Textures.Contains(null))
                Index = Textures.IndexOf(null);

            if (Index < 0)
            {
                GameTexture NewTexture = new GameTexture(TextureFile);
                NewTexture.Handle = Textures.Count;
                Textures.Add(NewTexture);
                return NewTexture;
            }
            else
            {
                Textures[Index] = new GameTexture(TextureFile);
                Textures[Index].Handle = Index;
                return Textures[Index];
            }
        }
        public static Int32 AddTextureI(String TextureFile)
        {
            string NameOfFile = Path.GetFileName(TextureFile);

            foreach (GameTexture Texture in Textures)
            {
                if (Texture != null)
                {
                    if (Texture.Name.Equals(NameOfFile))
                    {
                        Texture.AddRef();
                        return Textures.IndexOf(Texture);
                    }
                }
            }

            int Index = -1;

            if (Textures.Contains(null))
                Index = Textures.IndexOf(null);

            if (Index < 0)
            {
                GameTexture NewTexture = new GameTexture(TextureFile);
                NewTexture.Handle = Textures.Count;
                Textures.Add(NewTexture);
                return Textures.IndexOf(NewTexture);
            }
            else
            {
                Textures[Index] = new GameTexture(TextureFile);
                Textures[Index].Handle = Index;
                return Index;
            }
        }
        public static void ReleaseTexture(GameTexture ToDispose)
        {
            ReleaseTexture(ToDispose.Handle);
        }
        public static void ReleaseTexture(Int32 ToDispose)
        {
            if (Textures[ToDispose] != null)
                Textures[ToDispose].Release();
        }
        public static void ReleaseAllTextures()
        {
            foreach (GameTexture Texture in Textures)
                if (Texture != null)
                    Texture.Release();
        }
        public static void ClearTextures()
        {
            foreach (GameTexture Texture in Textures)
            {
                while (Texture.ReferenceCount > 0)
                    Texture.Release();
            }

            Textures.Clear();
        }
        public static void Draw(GameTexture TheTexture, Rectangle Dest, Color Overlay)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Dest, Overlay);
        }
        public static void Draw(GameTexture TheTexture, Vector2 Pos, Color Overlay)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Pos, Overlay);
        }
        public static void Draw(GameTexture TheTexture, Rectangle Dest, Rectangle Source, Color Overlay)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Dest, Source, Overlay);
        }
        public static void Draw(GameTexture TheTexture, Vector2 Pos, Rectangle Source, Color Overlay)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Pos, Source, Overlay);
        }
        public static void Draw(GameTexture TheTexture, Rectangle Dest, Rectangle Source, Color Overlay, Single Rotation, Vector2 Origin, SpriteEffects Effect, Single Depth)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Dest, Source, Overlay, Rotation, Origin, Effect, Depth);
        }
        public static void Draw(GameTexture TheTexture, Vector2 Pos, Rectangle Source, Color Overlay, Single Rotation, Vector2 Origin, Single Scale, SpriteEffects Effect, Single Depth)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Pos, Source, Overlay, Rotation, Origin, Scale, Effect, Depth);
        }
        public static void Draw(GameTexture TheTexture, Vector2 Pos, Rectangle Source, Color Overlay, Single Rotation, Vector2 Origin, Vector2 Scale, SpriteEffects Effect, Single Depth)
        {
            if (!TheTexture.IsNullResource)
                FCanvas.Draw(TheTexture.Texture, Pos, Source, Overlay, Rotation, Origin, Scale, Effect, Depth);
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////